<template>
  <div>
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
      <el-form-item label="电影名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="内容概括" prop="contents">
        <el-input type="textarea" v-model="form.contents"></el-input>
      </el-form-item>
      <el-form-item label="上映时间" prop="created">
        <el-date-picker v-model="form.created"></el-date-picker>
      </el-form-item>
      <el-form-item label="分类"  prop="tid">
        <el-select v-model="form.tid">
          <el-option v-for=" t in types" :key="t.id" :value="t.id" :label="t.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="导演"  prop="directIds">
        <el-checkbox-group v-model="form.directIds">
          <el-checkbox v-for="d in directs" :key="d.id" :label="d.id">{{ d.name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="主演"  prop="actorIds">
        <el-checkbox-group v-model="form.actorIds">
          <el-checkbox v-for="a in actors" :key="a.id" :label="a.id">{{ a.name }}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="宣传图">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:82/api/movie/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
          <img v-if="form.picture" :src="form.picture" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">保存</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'AddView',
  data () {
    return {
      form: {
        name: '',
        contents: '',
        created: '',
        tid: null,
        directIds: [],
        actorIds: [],
        picture: ''
      },
      types: [],
      directs: [],
      actors: [],
      rules: {
        name: [
          { required: true, message: '请输入电影名', trigger: 'blur' }
        ],
        contents: [
          { required: true, message: '请输入概述', trigger: 'blur' }
        ],
        created: [
          { required: true, message: '请选择上映时间', trigger: 'blur' }
        ],
        tid: [
          { required: true, message: '请选择类型', trigger: 'changer' }
        ],
        directIds: [
          { required: true, message: '请选择导演', trigger: 'changer' }
        ],
        actorIds: [
          { required: true, message: '请选择主演', trigger: 'changer' }
        ]
      }
    }
  },
  created () {
    this.listType()
    this.listDirect()
    this.listActor()
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post('/movie/save', this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success('添加成功')
              this.$router.push('/list')
            } else if (res.data.code === 201) {
              this.$message.error('姓名不能为空')
            } else if (res.data.code === 202) {
              this.$message.error('不能重复提交')
            }
          })
        } else {
          this.$message.error('信息不完整')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    handleAvatarSuccess (res, file) {
      this.form.picture = res.url
    },
    listType () {
      this.axios.get('/type/typeList').then(res => {
        this.types = res.data
      })
    },
    listDirect () {
      this.axios.get('/direct/list').then(res => {
        this.directs = res.data
      })
    },
    listActor () {
      this.axios.get('/actor/list').then(res => {
        this.actors = res.data
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
